//
//  NetWorkTools.swift
//  Weibo
//
//  Created by 周先生 on 15/11/20.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit
import AFNetworking

private let weiboDomain = "com.baidu.data.error"
//测试
private let hostname = "https://api.weibo.com/"
//swift中枚举类型可以是任意类型
enum HTTPMethod: String {
    case POST = "POST"
    case GET = "GET"

}
class NetWorkTools: AFHTTPSessionManager {

    //定义单例对象
    static let sharedTools: NetWorkTools = {
//        let urlString = "https://api.weibo.com/"
//        let url = NSURL(string: urlString)
        let url = NSURL(string: hostname)
        let tools = NetWorkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //定义网络访问方法 以后所有的网络访问都经过该方法调用AFN
    //加载字典数据
    func requestJsonDict(method: HTTPMethod, urlString: String, parameter: [String : AnyObject]?, finished: (result: [String : AnyObject]?, error: NSError?) -> ()) {
        //调用AFN的具体的POST GET方法
        if method == HTTPMethod.GET {
         GET(urlString, parameters: parameter, success: { (_, result) -> Void in
            
            //检验数据是否是字典
            if let dict = result as? [String: AnyObject] {
                //执行成功的回调
                finished(result: dict, error: nil)
                return
            
            }
            //数据信息不匹配 自定义错误信息 一般自定义errorcode为负值
            let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey: "数据错误"])
            finished(result: nil, error: dataError)
            }, failure: { (_, error) -> Void in
             //执行错误的回调
                finished(result: nil, error: error)
                print(error)
         })
        
        } else {
            POST(urlString, parameters: parameter, success: { (_, result) -> Void in
                
                //检验数据是否是字典
                if let dict = result as? [String: AnyObject] {
                    //执行成功的回调
                    finished(result: dict, error: nil)
                    return
                
                }
                //数据信息不匹配 自定义错误信息 一般自定义errorcode为负值
                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey: "数据错误"])
                finished(result: nil, error: dataError)

                }, failure: { (_, error) -> Void in
                    //执行错误的回调
                    finished(result: nil, error: error)
                    print(error)
            })
        }
    }
    //添加上传图片的方法
    func uploadImage(urlString: String, parameter: [String : AnyObject]?, imageData: NSData, finished: (result: [String : AnyObject]?, error: NSError?) -> ()) {
        POST(urlString, parameters: parameter, constructingBodyWithBlock: { (multipartFormData) -> Void in
            //1,data 要上传的文件的二进制数据
            //2,name 表示上传的文件服务器对应的字段
            //3,fileName 表示服务器接收后保存的名字名字可以随意取
            //4,上传文件的格式 image/jpeg
            multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "OMG", mimeType: "image/jpeg")

            }, success: { (_, result) -> Void in
                if let dict = result as? [String: AnyObject] {
                    finished(result: dict, error: nil)
                
                }
            }) { (_, error) -> Void in
                finished(result: nil, error: error)
                print(error)
        }
    
    }
}







